var cw_schema = "";

function resize_navigation()
{
	
	navigation_height = size['height'] - $('navigation').getStripStyle('top') - $('navigation').getStripStyle('margin-top') - $('navigation').getStripStyle('margin-bottom') - $('navigation').getStripStyle('padding-top') - $('navigation').getStripStyle('padding-bottom');
	
		$('navigation').setStyle({'height': (navigation_height)+'px'});			

}

function refresh_navigation()
{
		var url = '../app/navigation.php';
		
		new Ajax.Request(url, {
		  method: 'post',
		  postBody: 'action=refresh',
		  onSuccess: function(transport) {
			  
			  var temp = transport.responseText;
			  $('navigation').update(temp);
			  select_schema(schema);
		  }
		});		
}

function display_tables(schema)
{
	if(!$('schema_' + schema).hasClassName('expanded') && !$('schema_' + schema).select( 'ul#tables_' + schema).size())
	{
		var url = '../app/navigation.php';
		
		new Ajax.Request(url, {
		  method: 'post',
		  postBody: 'action=show_tables&schema=' + schema,
		  onSuccess: function(transport) {
			  
			  var temp = transport.responseText;
			  $('schema_'+schema).insert(temp, { position: 'bottom' });
			//  todos = $('result').firstDescendant();
			 // alert(todos.size());
			 
		  }
		});		
	}
	$('schema_' + schema).toggleClassName('expanded');
}

function display_columns(schema, table)
{
	if(!$('table_' + schema + '_' + table).hasClassName('expanded') && !$('table_' + schema + '_' + table).select( 'ul#columns_' + schema+'_'+table).size())
	{
		var url = '../app/navigation.php';
		
		new Ajax.Request(url, {
		  method: 'post',
		  postBody: 'action=show_columns&schema=' + schema +'&table=' + table,
		  onSuccess: function(transport) {
			  
			  var temp = transport.responseText;
			  $('table_'+schema+'_'+table).insert(temp, { position: 'bottom' });

			 
		  }
		});		
	}
	$('table_' + schema + '_' + table).toggleClassName('expanded');
}

function select_table(schema, table)
{
	
	if(cw_schema != schema)
	{
		query_str = "SELECT * FROM " + schema +"."+ table;
	}
	else
	{
		query_str = "SELECT * FROM " + table;	
	}

	add_query(query_str);

}

function select_schema(schema)
{
	if(cw_schema)
	{
		$('schema_' + cw_schema).toggleClassName('selected');	
	}
	cw_schema =  schema;
	$('schema_' + cw_schema).toggleClassName('selected');	
	
	display_tables(schema);
}
